/**
 * 
 */
package com.rtsp.tool.snatch05;

import java.awt.image.BufferedImage;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author w.weizhen
 *
 */
public class RunnableSave05 implements Runnable{
//	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		
//	}
	private String code;
	
	private final String user_dir = System.getProperty("user.dir") + File.separator;
	private final String data_path = user_dir + "data" + File.separator;
	private String dateTimeFormatter = "yyyy-MM-dd"+ File.separator +"HH-mm";
	RunnableSave05(String code)
	{
		this.code = code;
	}
	private String path(String code)
	{
		String file_absolute = data_path + code + File.separator + LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateTimeFormatter));
		File file = new File(file_absolute);
		if(!file.exists())
		{
			file.mkdirs();
		}
		return file_absolute;
	}
	
	@Override
	public void run() 
	{
		List<Map<String, Frame>> datas = RunnableMain05.datas.get(code);
		if(datas == null || datas.isEmpty() || datas.size() < 1)
		{
			return;
		}
		Map<String, Frame> map = datas.get(0);
		List<String> file_names = new LinkedList<>(map.keySet());
		List<Frame> frames = new LinkedList<>(map.values());
		
		String file_absolute = path(code);
		try {
			Java2DFrameConverter converter = new Java2DFrameConverter();
			BufferedImage bi = converter.getBufferedImage(frames.get(0));
			File outPut = new File(file_absolute + File.separator + file_names.get(0) + ".jpeg");
			ImageIO.write(bi, "jpeg", outPut);
			bi.flush();
			datas.remove(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}